<template>
  <div class="zy-content">
    <search :columns="searchData" @getSearch="getSearch(arguments)" ref="search"></search>
    <div class="alarmConfiguration">
      <div class="list-layout mt16">
        <div class="btns clear">
          <span class="title">生产订单</span>
          <!-- <Button @click="false">自动排程</Button> -->
          <Button v-has="'Manualscheduling'" @click="handleChange('add','hand')">手动排程</Button>
          <!--          <Button v-has="'Auxiliaryscheduling'" @click="fzpc()">辅助排程</Button>-->
          <Button v-has="'Auxiliaryscheduling'" @click="handleChange('add','auto')">辅助排程</Button>
        </div>
        <div class="tableContent">
          <!-- {{ delIds }} -->
          <!-- @on-column-width-resize="onColumnWidthResize" -->
          <Table
              class="resizable-table"
              border
              size="small"
              highlight-row
              @on-column-width-resize="
							(a, b, c, d) => {
								onColumnWidthResize(a, b, c, d, 'orderAdjustmentDeviceInfo');
							}
						"
              :columns="orderAdjustmentDeviceInfoList"
              ellipsis
              ref="selection"
              stripe
              :data="dataArr"
              @on-cell-click="cellHandle"
              @on-selection-change="onSelectChange"
              @on-row-click="rowClick"
          >
            <!-- <template #hourQuota="{ row, column }">
              <InputNumber ref="tableInput" :max="20" :min="0" :step="1" v-model="row.hourQuota"
                v-if="isEdit === column.slot + '-' + row._index" @on-blur="isEdit = ''" />
              <span v-else>{{ row.hourQuota }}</span>
            </template>-->
            <template slot-scope="{ row }" slot="action">
              <span class="operationBtn" @click="viewNumble(row.orderCode)">查看物料用量</span>
            </template>
          </Table>
        </div>
        <!-- 分页 -->
        <div class="pageWrap mt16">
          <pageLeftTitle :total="total" :selected="delIds.length"/>
          <Page
              :total="total"
              :page-size="pageSize"
              :page-size-opts="pageSizeOption"
              :current="pageNum"
              @on-change="changePageNum"
              @on-page-size-change="changePageSize"
              show-sizer
              show-total
          />
        </div>
      </div>
      <!-- echart -->
      <div class="list-layout echart_Box mt16">
        <div class="btns clear">
          <span class="title">排程视图</span>
          <span style="margin-left: 10px" v-for="(item, index) in stateColor" :key="index">
						<span style="font-size: 14px">{{ item.label }}</span>
						<span style="width: 10px; height: 10px; margin-left: 8px" :style="'background:' + item.value"></span>
					</span>
        </div>
        <!-- <Date-picker class="selectCy" style="width: 200px; font-size: 10px" type="daterange"></Date-picker> -->
        <div v-for="(item, index) in echartData" :key="index" class="one_line mt16">
          <Tooltip :content="item.deviceName">
            <div class="device_name">{{ item.deviceName }}:</div>
          </Tooltip>

          <div class="echart" v-if="loading == false">
            <oneLineChart :index="'oneLine' + index" :mock="item.list" :deviceCode="item.deviceCode"/>
          </div>
        </div>
        <div class="timeAxis">
          <span class="startTime">{{ startTime }}</span>
          <span class="endTime">{{ endTime }}</span>
        </div>
      </div>
      <div class="list-layout mt16">
        <div class="btns clear">
          <span class="title">已排程工单</span>
          <Button @click="exportTemplateClick">{{ $t('common.Export') }}</Button>
          <Button  v-has="'PrintWorkCard'" @click="rowPrint()">工单打印</Button>
          <Button v-has="'Distribute'" @click="distribute(true)">下发</Button>
          <Button v-has="'Recallscheduling'" @click="distribute(false)">撤回排程</Button>
          <Button v-has="'modify'" @click="editOrder('edit')">修改</Button>
          <Button v-has="'Scheduledelete'" @click="delOrder">删除</Button>
        </div>
        <!-- 表格 -->
        <!-- @on-column-width-resize="onColumnWidthResize" -->
        <div class="tableContent">
          <Table
              class="resizable-table"
              border
              size="small"
              @on-column-width-resize="onColumnWidthResize"
              :columns="columnsK"
              ellipsis
              ref="selection"
              stripe
              :data="alData"
              @on-selection-change="onSelectChange1"
          >
            <template slot-scope="{ row }" slot="action">
              <span class="operationBtn" @click="viewNumble(row.workCode, true)">查看物料用量</span>
            </template>
          </Table>
          <div class="pageWrap mt16">
            <pageLeftTitle :total="alTotal" :selected="delIds1.length"/>
            <Page
                :total="alTotal"
                :page-size="alPageSize"
                :page-size-opts="pageSizeOption"
                :current="alPageNum"
                @on-page-size-change="alChangePageSize"
                @on-change="alChangePageNum"
                show-sizer
                show-total
            />
          </div>
        </div>
      </div>
      <!-- 排程弹窗 -->
      <Modal :mask-closable="false" v-model="create" :title="diaStatus == 'edit' ? '编辑' : '排程'"
             class="modal-content-layout" width="80%">
        <div class="searchBox search-box-layout">
          <Form ref="form" class="formList" :model="formItem" label-position="top" :rules="ruleValidate">
            <Row :gutter="24">
              <Col span="6">
                <FormItem label="工单开始时间：" prop="workStartTime">
                  <DatePicker
                      type="datetime"
                      format="yyyy-MM-dd HH:mm:ss"
                      :value="formItem.workStartTime"
                      placeholder="请选择工单开始时间："
                      :clearable="true"
                      name="sxbegdate"
                      @on-change="getbegdate"
                  ></DatePicker>
                </FormItem>
              </Col>
              <Col span="6">
                <FormItem label="工单结束时间：" prop="workEndTime">
                  <DatePicker
                      type="datetime"
                      format="yyyy-MM-dd HH:mm:ss"
                      :value="formItem.workEndTime"
                      :clearable="true"
                      placeholder="请选择结束时间"
                      name="gqenddate"
                      @on-change="getenddate"
                  ></DatePicker>
                </FormItem>
              </Col>
              <Col span="4">
                <FormItem label="计划生产数量：" prop="planProduceQuantity">
                  <InputNumber style="width: 100%" :min="1" v-model="formItem.planProduceQuantity"></InputNumber>
                </FormItem>
              </Col>
              <Col span="4" prop="capacityPerHour">
                <FormItem label="每小时产能：" prop="capacityPerHour">
                  <InputNumber style="width: 100%" :min="1" v-model="formItem.capacityPerHour"></InputNumber>
                </FormItem>
              </Col>
              <Col span="4" v-if="schedulingType=='auto'">
                <Button type="primary" style="margin: 20px" @click="computeAutoTime">计算</Button>
              </Col>
              <Col span="4" v-else>
                <Button type="primary" style="margin: 20px" @click="computeTime">计算</Button>
              </Col>
            </Row>
          </Form>
        </div>
        <div class="tableContent">
          <Row :gutter="20" class="mt16">
            <Col span="12">
              <!-- {{ moldListRow }} -->
              <Table
                  class="resizable-table"
                  border
                  size="small"
                  :columns="columns1"
                  ellipsis
                  highlight-row
                  ref="selection"
                  stripe
                  :data="moldList"
                  @on-row-click="onRowClick"
              ></Table>
            </Col>
            <Col span="12">
              <!-- {{ rows }} -->
              <Table
                  class="resizable-table"
                  border
                  size="small"
                  :columns="columns2"
                  ellipsis
                  ref="selection"
                  stripe
                  :data="deviceList"
                  @on-selection-change="dataClick"
              ></Table>
            </Col>
          </Row>
        </div>
        <div slot="footer">
          <Button type="default" @click="(create = false), (rows = {}), (selectRow = false)">{{
              $t('common.Cancle')
            }}
          </Button>
          <!-- {{ diaStatus }} -->
          <Button type="primary" :loading="loading" @click="addOk('form')">
            {{ $t('maintenance.Confirmed') }}
          </Button>
        </div>
      </Modal>
      <!-- 排程设置 -->
      <Modal :mask-closable="false" v-model="setting" title="排程设置" class="modal-content-layout" width="30%">
        <Form ref="form" class="formList" :model="formItem" label-position="top" :rules="ruleValidate">
          <Row :gutter="24">
            <Col span="12">
              <FormItem label="交期条件" prop="workStartTime">
                <Checkbox v-model="settingObj.workStartTime">判断交期因素</Checkbox>
              </FormItem>
            </Col>
            <Col span="12">
              <FormItem label="换模条件" prop="workStartTime">
                <Checkbox v-model="settingObj.cc">判断换模因素</Checkbox>
              </FormItem>
            </Col>
            <Col span="12">
              <FormItem label="判断模具产品关联" prop="workStartTime">
                <Checkbox v-model="settingObj.bb">模具匹配条件</Checkbox>
              </FormItem>
            </Col>
            <Col span="12">
              <FormItem label="判断模具设备关联" prop="workStartTime">
                <Checkbox v-model="settingObj.aa">设备匹配条件</Checkbox>
              </FormItem>
            </Col>
          </Row>
        </Form>
        <div slot="footer">
          <Button type="default" @click="setting = false">
            {{ $t('common.Cancle') }}
          </Button>
          <!-- {{ diaStatus }} -->
          <Button type="primary" :loading="loading" @click="addOk('form')">
            {{ $t('maintenance.Confirmed') }}
          </Button>
        </div>
      </Modal>
      <!-- 查看物料用量 -->
      <Modal :mask-closable="false" v-model="viewNumbleDialog" title="库存分析" class="modal-title-layout" width="80%">
        <div class="list-layout mt16">
          <!-- 表格 -->
          <div class="tableContent">
            <Table
                class="resizable-table"
                border
                size="small"
                @on-column-width-resize="onColumnWidthResize"
                :columns="viewNumbleColumns"
                ellipsis
                ref="selection"
                stripe
                :data="inventoryAnalysisData"
            ></Table>
          </div>
          <div class="pageWrap mt16">
            <pageLeftTitle :total="inventoryTotal"/>
            <Page
                :total="inventoryTotal"
                :page-size="inventoryPageSize"
                :page-size-opts="pageSizeOption"
                :current="inventoryPageNum"
                @on-page-size-change="inventoryChangePageSize"
                @on-change="inventoryChangePageNum"
                show-sizer
                show-total
            />
          </div>
        </div>
        <div slot="footer">
          <Button type="default" @click="viewNumbleDialog = false">
            {{ $t('common.Cancle') }}
          </Button>
          <Button type="primary" :loading="loading" @click="viewNumbleDialog = false">{{
              $t('maintenance.Confirmed')
            }}
          </Button>
        </div>
      </Modal>
      <Modal :mask-closable="false" v-model="distributeView" :title="distributeTitle ? '下发确认' : '撤回排程'"
             class="modal-title-layout">
        <div class="icon blue" v-if="distributeTitle == true">
          <i class="iconfont zy-common-wancheng"></i>
        </div>
        <div class="icon red" v-else>
          <i class="iconfont zy-common-shanchu"></i>
        </div>
        <p class="title">
          <span v-if="distributeTitle == true">是否确认工单下发生产</span>
          <span v-else>是否撤回工单下发生产</span>
        </p>
        <div slot="footer">
          <Button type="default" @click="distributeView = false">
            {{ $t('common.Cancle') }}
          </Button>
          <Button type="primary" :loading="loading" @click="distributeApi">
            {{ $t('maintenance.Confirmed') }}
          </Button>
        </div>
      </Modal>
      <commonModal ref="commonModal"></commonModal>
    </div>
    <ledger-print-modal ref="ledgerPrintModal"/>

  </div>
</template>

<script>
import {
  searchApi,
  getDeviceList,
  getMoldList,
  add,
  deleteApi,
  assistedScheduling
} from '@/api/planManage/orderSchedul.js';
import search from '@/components/curd/searchList.vue';
import oneLineChart from './moduls/oneLineChart.vue';
import commonModal from '@/components/modalTypes/commonModal.vue';
import fileDownload from 'js-file-download';
import toggleTableOperation from '@/mixins/toggleTableOperation';
import ledgerPrintModal from "./moduls/ledgerPrintModal";

export default {
  name: 'productionPlan',
  components: {search, oneLineChart, commonModal, ledgerPrintModal},
  mixins: [toggleTableOperation],
  data() {
    return {
      schedulingType: 'hand',//hand手动 auto辅助排程
      rowInfo: {},
      stateColor: [
        {
          label: '空闲',
          value: '#1db360', //绿色
        },
        {
          label: '已排程',
          value: '#f21d40', //绿色
        },
      ],
      settingObj: {},
      alTotal: 0,
      alPageSize: 10,
      alPageNum: 1,
      alData: [],
      alDel: [],
      viewNumbleDialog: false,
      ruleValidate: {
        workStartTime: [
          {
            required: true,
            message: '请选择开始时间',
            trigger: 'blur',
          },
        ],
        workEndTime: [
          {
            required: true,
            message: '请选择结束时间',
            trigger: 'blur',
          },
        ],
        planProduceQuantity: [
          {
            required: true,
            message: '请填写计划生产数量',
            trigger: 'blur',
            type: 'number',
          },
        ],
        capacityPerHour: [
          {
            required: true,
            message: '请填写每小时产能',
            trigger: 'blur',
            type: 'number',
          },
        ],
      },
      setting: false,
      formItem: {
        workStartTime: '',
        workEndTime: '',
        planProduceQuantity: 0,
        capacityPerHour: 0,
      },
      searchData: [
        {
          title: '作业工序',
          key: 'productionProcesses',
        },
        {
          title: '车间',
          key: 'workShop',
        },
        {
          title: '订单号',
          key: 'orderCode',
        },
        {
          title: '状态',
          key: 'stateList',
          type: 'select',
          multiple: true,
          maxTagCount: 1,
          dicData: [
            {label: '待排产', value: '待排产'},
            {label: '完成', value: '完成'},
            {label: '已结案', value: '已结案'},
            {label: '已排产', value: '已排产'},
            {label: '取消结案', value: '取消结案'},
          ],
        },
        {
          title: '产品编号',
          key: 'orderMaterialCode',
        },
        {
          title: '产品名称',
          key: 'materialName',
        },
        {
          title: '图号规格',
          key: 'customMaterialCode',
        },
        {
          title: 'MRP创建者',
          key: 'mrpController',
        },
        {
          title: '销售订单号',
          key: 'saleOrderCode',
        },
        {
          title: '备注1',
          key: 'remarks1',
        },
        {
          title: '备注2',
          key: 'remarks2',
        },
        {
          title: '排程人',
          key: 'handProduceMan',
          dicUrl: '/basic/user/searchUser',
          type: 'search',
          queryName: 'key',
          resultParams: 'res.data',
          params: {
            label: 'realname',
            value: 'realname',
          },
        },
        {
          title: '排程时间',
          key: 'time',
          span: '6',
          type: 'datetimerange',
          format: 'yyyy-MM-dd HH:mm:ss',
        },
        {
          title: '创建时间',
          key: 'time2',
          format: 'yyyy-MM-dd',
          type: 'daterange',
          span: 6,
          defaultTimeRange: 30,
          noreset: true,
          clearable: false,
        },
        {
          title: '订单时间完成时间',
          key: 'time3',
          span: 6,
          type: 'datetimerange',
          format: 'yyyy-MM-dd HH:mm:ss',
        },
        {
          title: '下发时间',
          key: 'time2',
          span: '6',
          type: 'datetimerange',
          format: 'yyyy-MM-dd HH:mm:ss',
        },
        {
          title: '下发人',
          key: 'distributeMan',
          dicUrl: '/basic/user/searchUser',
          type: 'search',
          queryName: 'key',
          resultParams: 'res.data',
          params: {
            label: 'realname',
            value: 'realname',
          },
        },
      ],
      columns: [
        {
          key: 'checked',
          type: 'selection',
          width: 60,
          fixed: 'left',
          align: 'center',
        },
        {
          title: '销售订单号',
          key: 'saleOrderCode',
          minWidth: 130,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '销售订单行号',
          key: 'saleOrderItem',
          minWidth: 130,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '订单号',
          key: 'orderCode',
          minWidth: 90,
          align: 'center',
          resizable: true,
          tooltip: true,
        },

        {
          title: '工序',
          key: 'productionProcesses',
          minWidth: 120,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '订单组号',
          key: 'workOrderGroup',
          minWidth: 150,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '车间',
          key: 'workShop',
          minWidth: 100,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '客户',
          key: 'custom',
          minWidth: 100,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '产品编号',
          key: 'orderMaterialCode',
          minWidth: 150,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '产品名称',
          key: 'materialName',
          minWidth: 100,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '产品型号',
          key: 'materialModel',
          minWidth: 100,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '图号规格',
          key: 'customMaterialCode',
          minWidth: 100,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '材料牌号',
          key: 'materialMark',
          minWidth: 100,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '计划数量',
          key: 'planQuantity',
          minWidth: 100,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '提报数量',
          key: 'reportedQuantity',
          minWidth: 100,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '报废数',
          key: 'outNumTotal',
          minWidth: 100,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '领料数',
          key: 'operationQuantity',
          width: 100,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '状态',
          key: 'state',
          minWidth: 80,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '计划开始时间',
          key: 'planStartDate',
          minWidth: 130,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '计划完成时间',
          key: 'planFinishDate',
          minWidth: 130,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '实际完成时间',
          key: 'orderFinishTime',
          width: 130,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '计划交期',
          key: 'deliverDate',
          minWidth: 100,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        // {
        //   title: "工序设定作业时间",
        //   key: "hourQuota",
        //   minWidth: 150,
        //   align: "center",
        //   resizable: true, tooltip: true,
        //   slot: "hourQuota",
        // },
        {
          title: '已排程数量',
          key: 'workOrderSum',
          minWidth: 130,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: 'mes创建时间',
          key: 'createTime',
          minWidth: 130,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '维护人',
          key: 'createBy',
          minWidth: 90,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: 'MRP创建者',
          key: 'mrpController',
          width: 130,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '备注1',
          key: 'remarks1',
          width: 130,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '备注2',
          key: 'remarks2',
          width: 130,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '备注3',
          key: 'remarks3',
          width: 130,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '备注4',
          key: 'remarks4',
          width: 130,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '备注5',
          key: 'remarks5',
          width: 130,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '油品分类',
          key: 'oilClassification',
          width: 130,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '客户牌号',
          key: 'customMark',
          width: 130,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '超报比例',
          key: 'overreportingRatio',
          minWidth: 110,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '操作',
          slot: 'action',
          fixed: 'right',
          minWidth: 120,
          align: 'center',
        },
      ],
      columnsK: [
        {
          key: 'selection',
          type: 'selection',
          width: 60,
          align: 'center',
        },
        {
          title: '工单号',
          key: 'workCode',
          minWidth: 90,
          align: 'center',
          resizable: true,
          tooltip: true,
        },

        {
          title: '产品编号',
          key: 'materialCode',
          minWidth: 100,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '工单组号',
          key: 'groupItem',
          minWidth: 150,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '设备编号',
          key: 'deviceCode',
          minWidth: 100,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '模具编号',
          key: 'moldCode',
          minWidth: 100,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '模具使用状态',
          key: 'moldState',
          minWidth: 130,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        // {
        //   title: "班次",
        //   key: "cla",
        //   minWidth: 100,
        //   align: "center",
        //   resizable: true, tooltip: true,
        // },
        {
          title: '计划数量',
          key: 'planProduceQuantity',
          minWidth: 100,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '提报数量',
          key: 'reportedQuantity',
          minWidth: 100,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '生产数量',
          key: 'produceQuantity',
          minWidth: 100,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '图号规格',
          key: 'customMaterialCode',
          minWidth: 100,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '材料牌号',
          key: 'materialMark',
          minWidth: 100,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '状态',
          key: 'state',
          minWidth: 90,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '工单开始时间',
          key: 'workStartTime',
          minWidth: 130,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '工单结束时间',
          key: 'workEndTime',
          minWidth: 130,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '工单实际完成时间',
          key: 'produceEndTime',
          minWidth: 150,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '生产顺序',
          key: 'sort',
          minWidth: 100,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '客户',
          key: 'custom',
          minWidth: 90,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '排程人',
          key: 'handProduceMan',
          minWidth: 90,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '排程时间',
          key: 'handProduceTime',
          minWidth: 150,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '下发人',
          key: 'distributeMan',
          minWidth: 90,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '下发时间',
          key: 'distributeTime',
          minWidth: 150,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '操作',
          slot: 'action',
          fixed: 'right',
          minWidth: 120,
          align: 'center',
        },
      ],
      columns1: [
        {
          title: '模具编号',
          key: 'moldCode',
          width: 90,
          align: 'center',
          resizable: true,
          tooltip: true,
        },

        {
          title: '模具名称',
          key: 'moldName',
          width: 90,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '生产模数',
          key: 'productionMode',
          width: 90,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '模具状态',
          key: 'moldState',
          width: 90,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '标准型腔数',
          key: 'standardCavityNum',
          width: 110,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '实际使用型腔数',
          key: 'realityCavityNum',
          width: 120,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
      ],
      columns2: [
        {
          key: 'selection',
          type: 'selection',
          width: 60,
          align: 'center',
        },
        {
          title: '优先级',
          key: 'priority',
          width: 80,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        // {
        //   title: "机台编号",
        //   key: "assetCode",
        //   minWidth: 100,
        //   align: "center",
        //   resizable: true,tooltip: true,
        // },
        {
          title: '设备编号',
          key: 'deviceCode',
          width: 100,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '设备名称',
          key: 'deviceName',
          width: 100,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '设备状态',
          key: 'deviceState',
          width: 100,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '工单数',
          key: 'workOrderNumber',
          width: 80,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '最早开始时间',
          key: 'earliestStartTime',
          width: 130,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '最晚结束时间',
          key: 'lateEndTime',
          width: 130,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
      ],
      inventoryAnalysisData: [],
      inventoryPageNum: 1,
      inventoryPageSize: 5,
      inventoryTotal: 0,
      viewNumbleColumns: [
        {
          title: '序号',
          type: 'index',
          width: 80,
          align: 'center',
        },
        {
          title: '物料编号',
          key: 'materialCode',
          minWidth: 60,
          align: 'center',
          resizable: true,
          tooltip: true,
        },

        {
          title: '物料名称',
          key: 'materialName',
          minWidth: 100,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '物料类别',
          key: 'materialType',
          minWidth: 100,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        // {
        //   title: "所属仓库",
        //   key: "moldName",
        //   minWidth: 100,
        //   align: "center",
        //   resizable: true, tooltip: true,
        // },
        // {
        //   title: "现有库存存量",
        //   key: "moldModel",
        //   minWidth: 100,
        //   align: "center",
        //   resizable: true,
        //   tooltip: true,
        // },
        {
          title: '需求数量',
          key: 'needQuantity',
          minWidth: 100,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '计量单位',
          key: 'unit',
          minWidth: 100,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '材料牌号',
          key: 'materialMark',
          minWidth: 100,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
        {
          title: '是否替代料',
          key: 'isReplaceMaterial',
          minWidth: 100,
          align: 'center',
          resizable: true,
          tooltip: true,
        },
      ],
      selectRow: false,
      rows: {},
      orderCode: '',
      moldList: [],
      deviceList: [],
      isEdit: false,
      allCheck: false,
      loading: false,
      create: false,
      distributeView: false,
      searchParams: {},
      pageNum: 1,
      pageSize: 5,
      total: 0,
      dataArr: [],
      delFaultCodeShowF: false, //是否显示删除弹窗
      pageSizeOption: [5, 10, 20],
      delIds: [],
      delIds1: [],
      selection: [],
      distributeTitle: '',
      echartData: [],
      moldListRow: {},
      data: [],
      diaStatus: '',
      cycle: [],
    };
  },
  mounted() {
    this.getTableColumnsWidth(this.columns, 'orderAdjustmentDeviceInfo');
    this.getTableColumnsWidth(this.columnsK);
    this.$refs.search.getSearch();
    // this.getAlList();
  },
  methods: {
    delOrder() {
      const data = this.delIds1.map((item) => item.id);
      if (data.length == 0) {
        this.$Message.warning({
          content: '至少选择一条已排程工单',
          duration: 6,
          closable: true,
        });
        return;
      }
      this.$refs.commonModal.showModal({
        type: 'delete',
        title: '删除提醒',
        desc: '确认是否删除',
        submit: () => {
          deleteApi(data).then((res) => {
            if (res.code === 200) {
              this.$Message.success(res.msg);
              this.delIds1 = [];
              this.refresh();
              // this.getAlList();
            } else {
              this.$Message.warning({
                content: res.msg,
                duration: 6,
                closable: true,
              });
            }
          });
        },
      });
    },
    getSearch(params) {
      this.searchParams = JSON.parse(JSON.stringify(params[0]));
      const {time, time2, time3} = this.searchParams;
      if (time && time.length) {
        this.searchParams.handProduceStartTime = time[0];
        this.searchParams.handProduceEndTime = time[1];
        delete this.searchParams.time;
      }
      if (time2 && time2.length) {
        this.searchParams.startTime = time2[0];
        this.searchParams.endTime = time2[1];
        delete this.searchParams.time2;
      }
      if (time3 && time3.length) {
        this.searchParams.orderFinishTimeStart = time3[0];
        this.searchParams.orderFinishTimeEnd = time3[1];
        delete this.searchParams.time3;
      }
      this.searchList();
      this.pageNum = 1;
      this.alData = [];
    },
    viewNumble(code, type) {
      this.inventoryMaterialCode = code;
      this.getInventoryList(type);
    },
    getInventoryList(type) {
      let params = {
        pageNum: this.inventoryPageNum,
        pageSize: this.inventoryPageSize,
        workCode: this.inventoryMaterialCode,
        orderCode: this.inventoryMaterialCode,
      };
      type ? delete params.orderCode : delete params.workCode;
      this.axios({
        method: 'get',
        url: type ? '/dms/productionWork/workInventoryAnalysis' : '/dms/productionWork/inventoryAnalysis',
        params,
      }).then((res) => {
        if (res.code === 200) {
          this.inventoryAnalysisData = res.data.records;
          this.inventoryTotal = res.data.total;
          this.viewNumbleDialog = true;
        } else {
          this.$Message.warning({
            content: res.msg,
            duration: 6,
            closable: true,
          });
        }
      });
    },
    getEchartData() {
      this.loading = true;
      this.axios({
        method: 'get',
        url: '/dms/productionWork/produceView',
        params: {
          orderCode: this.orderCode,
          status: 1
        },
      }).then((res) => {
        if (res.code === 200) {
          this.$set(this, 'echartData', res.data);
          // this.echartData = res.data;
          if (res.data.length > 0) {
            this.startTime = this.$moment(res.data[0].list[0].startTime).format('YYYY-MM-DD HH:mm');
            this.endTime = this.$moment(res.data[0].list[res.data[0].list.length - 1].endTime).format('YYYY-MM-DD HH:mm');
          }
          this.$forceUpdate();
        } else {
          this.$Message.warning({
            content: res.msg,
            duration: 6,
            closable: true,
          });
        }
        this.loading = false;
      });
    },
    rowPrint() {
      if (this.delIds1.length == 0) {
        this.$Message.warning({
          content: '请选择一条数据',
          duration: 6,
          closable: true,
        });
        return;
      }
      let arr = {
        loop: this.delIds1,
        info: this.rowInfo
      }
      console.log(arr, '打印参数')
      this.$refs.ledgerPrintModal.show(arr, 1);
    },
    distribute(status) {
      if (this.delIds1.length < 1) {
        this.$Message.warning({
          content: '只能选择一条数据',
          duration: 6,
          closable: true,
        });
        return;
      }
      this.distributeTitle = status;
      this.distributeView = true;
    },
    //辅助排程
    computeAutoTime() {
      if (this.rows.length == 0) {
        this.$Message.warning({
          content: '请选择一条数据',
          duration: 6,
          closable: true,
        });
        return;
      }
      if (this.rows.length > 1) {
        this.$Message.warning({
          content: '只能选择一条数据',
          duration: 6,
          closable: true,
        });
        return;
      }

      const param = {
        planCount: this.formItem.planProduceQuantity,//计划数量
        orderMaterialCode: this.delIds[0].orderMaterialCode,//物料编码
        moldCode: this.moldListRow.moldCode,//模具名称
        deviceNo: this.rows[0].deviceCode,//设备编号
        capacityPerHour: this.formItem.capacityPerHour//每小时产能
      }
      this.axios({
        url: '/dms/productionWork/assistedScheduling',
        method: 'post',
        data: param,
      }).then((res) => {
        if (res.code == 200) {
          this.formItem.workStartTime = res.data.startDateTime
          this.formItem.workEndTime = res.data.endDateTime
        } else {
          this.$Message.error(res.msg);
        }
      });
      // assistedScheduling(param).then(res => {
      //
      // })

      // let data = {
      //   workStartTime: this.formItem.workStartTime,
      //   planProduceQuantity: this.formItem.planProduceQuantity,
      //   moldCode: this.moldListRow.moldCode,
      //   materialCode: this.delIds[0].orderMaterialCode,
      //   orderCode: this.delIds[0].orderCode,
      // };
      // this.axios({
      //   url: '/dms/productionWork/labourArranged',
      //   method: 'post',
      //   data: data,
      // }).then((res) => {
      //   if (res.code == 200) {
      //     this.$set(this.formItem, 'workEndTime', res.data);
      //   } else {
      //     this.$Message.error(res.msg);
      //   }
      // });
    },
    computeTime() {
      if (this.rows.length == 0) {
        this.$Message.warning({
          content: '请选择一条数据',
          duration: 6,
          closable: true,
        });
        return;
      }
      if (this.rows.length > 1) {
        this.$Message.warning({
          content: '只能选择一条数据',
          duration: 6,
          closable: true,
        });
        return;
      }

      let data = {
        workStartTime: this.formItem.workStartTime,
        planProduceQuantity: this.formItem.planProduceQuantity,
        moldCode: this.moldListRow.moldCode,
        materialCode: this.delIds[0].orderMaterialCode,
        orderCode: this.delIds[0].orderCode,
      };
      this.axios({
        url: '/dms/productionWork/labourArranged',
        method: 'post',
        data: data,
      }).then((res) => {
        if (res.code == 200) {
          this.$set(this.formItem, 'workEndTime', res.data);
        } else {
          this.$Message.error(res.msg);
        }
      });
    },
    checkDeviceAndMoldStatus() {
      let moldStatus = true;
      let deviceStatus = false;
      if (this.moldListRow.moldState !== '正常') {
        moldStatus = false;
      } else moldStatus = true;
      for (let index = 0; index < this.rows.length; index++) {
        const element = this.rows[index];
        if (element.deviceState !== '正常') {
          deviceStatus = false;
        }
      }
      if (moldStatus == false || deviceStatus == false) {
        return new Promise((resolve, reject) => {
          this.$refs.commonModal.showModal({
            type: 'sure',
            title: '确认提醒',
            desc:
                moldStatus == false
                    ? `模具${this.moldListRow.moldCode}状态为${this.moldListRow.moldState}，是否继续？`
                    : `设备${this.rows[0].deviceCode}状态为${this.rows[0].deviceState}，是否继续？`,
            submit: () => {
              resolve(true);
            },
            cancel: () => {
              reject(false);
            },
          });
        });
      } else return true;
    },
    async addOk(name) {
      if (this.rows.length == 0) {
        this.$Message.warning({
          content: '请选择一条数据',
          duration: 6,
          closable: true,
        });
        return;
      }
      if (this.rows.length > 1) {
        this.$Message.warning({
          content: '只能选择一条数据',
          duration: 6,
          closable: true,
        });
        return;
      }
      const isConfirm = await this.checkDeviceAndMoldStatus();
      if (!isConfirm) {
        return;
      }
      this.$refs[name].validate((valid) => {
        if (!valid) {
          return;
        }
        this.loading = true;
        let data = {};
        if (this.diaStatus == 'add') {
          data = {
            ...this.formItem,
            orderCode: this.delIds[0].orderCode,
            moldCode: this.moldListRow.moldCode,
            deviceCode: this.rows[0].deviceCode,
          };
          if (this.delIds[0].workOrderGroup) {
            data.workOrderGroup = this.delIds[0].workOrderGroup;
          }
        } else {
          data = {
            ...this.formItem,
            id: this.delIds1[0].id,
            orderCode: this.delIds1[0].orderCode,
            moldCode: this.moldListRow.moldCode,
            deviceCode: this.rows[0].deviceCode,
          };
          if (this.delIds1[0].workOrderGroup) {
            data.workOrderGroup = this.delIds1[0].workOrderGroup;
          }
        }
        add(data, this.diaStatus).then((res) => {
          if (res.code === 200) {
            // this.create = false;
            this.loading = false;
            this.selectRow = false;

            this.delIds1 = [];
            this.delIds = [];

            this.create = false;
            this.refresh();
            this.$refs.form.resetFields();
            this.$Message.success({
              content: res.msg,
              duration: 6,
              closable: true,
            });
          } else {
            this.loading = false;
            this.selectRow = true;
            this.$Message.warning({
              content: res.msg,
              duration: 6,
              closable: true,
            });
          }
        });
      });
    },
    getenddate(val) {
      this.formItem.workEndTime = val;
    },

    getbegdate(val) {
      this.formItem.workStartTime = val;
    },
    dataClick(selection) {
      this.rows = selection;
    },
    changeNum(data) {
      this.pageNum = 1;
      this.searchParams = data;
      this.searchList();
    },
    changePageSize(pageSize) {
      this.pageSize = pageSize;
      this.searchList();
    },
    changePageNum(val) {
      this.pageNum = val;
      this.searchList();
    },
    alChangeNum(val) {
      this.alPageNum = 1;
      this.getAlList();
    },
    alChangePageSize(pageSize) {
      this.alPageSize = pageSize;
      this.getAlList();
    },
    alChangePageNum(val) {
      this.alPageNum = val;
      this.getAlList();
    },
    inventoryChangeNum(val) {
      this.inventoryPageNum = 1;
      this.getInventoryList();
    },
    inventoryChangePageSize(pageSize) {
      this.inventoryPageSize = pageSize;
      this.getInventoryList();
    },
    inventoryChangePageNum(val) {
      this.inventoryPageNum = val;
      this.getInventoryList();
    },
    getAlList() {
      let params = {
        pageNum: this.alPageNum,
        pageSize: this.alPageSize,
        orderCode: this.orderCode,
      };
      this.axios({
        method: 'get',
        url: '/dms/productionWork/arrangedWorkList',
        params,
      }).then((res) => {
        if (res.code === 200) {
          this.alData = res.data.records;
          this.alTotal = res.data.total;
        } else {
          this.$Message.warning({
            content: res.msg,
            duration: 6,
            closable: true,
          });
        }
      });
    },
    distributeApi() {
      let myArray = [];
      this.delIds1.map((item, index) => {
        myArray.push(item.id);
      });
      this.axios({
        method: 'PUT',
        url: this.distributeTitle ? '/dms/productionWork/workdistribute' : '/dms/productionWork/revokeArrange',
        data: myArray,
      }).then((res) => {
        if (res.code === 200) {
          this.distributeView = false;
          this.$Message.success({
            content: res.msg,
            duration: 6,
            closable: true,
          });
          this.delIds1 = [];
          this.refresh();
        } else {
          this.$Message.warning({
            content: res.msg,
            duration: 6,
            closable: true,
          });
        }
      });
    },
    onSelectChange(selection) {
      let myArray = [];

      selection.map((item, index) => {
        myArray.push(item);
      });
      this.delIds = myArray;
    },
    onSelectChange1(selection) {
      // let myArray = [];
      // selection.map((item, index) => {
      //   myArray.push(item);
      // });
      this.delIds1 = JSON.parse(JSON.stringify(selection));
    },
    rowClick(row) {
      this.orderCode = row.orderCode;
      this.rowInfo = JSON.parse(JSON.stringify(row))
      this.delIds1 = [];
      this.getEchartData();
      this.getAlList();
    },
    refresh() {
      this.getEchartData();
      this.searchList();
      this.getAlList();
      this.rows = [];
    },
    cellHandle(row, column) {
      // let arr = [0,1,2,3];     // 可以使用列名的.length
      // if(arr.indexOf(row._index) >= 0) return      // 当点击的是0，1,2,3行的时候，不给this.isEdit赋值，即无法完成切换效果
      // if(arr.indexOf(column._index) >= 0) return      // 当点击的是0，1,2,3列的时候，不给this.isEdit赋值，即无法完成切换效果
      // this.isEdit = column.slot + "-" + row._index;
      // this.$nextTick(() => {
      //   this.$refs.tableInput && this.$refs.tableInput.focus();
      // });
    },
    onRowClick(row) {
      this.moldListRow = row;
      this.rows = [];
      getDeviceList({moldCode: row.moldCode}).then((res) => {
        if (res.code === 200) {
          this.deviceList = res.data;
        } else {
          this.$Message.warning({
            content: res.msg,
            duration: 6,
            closable: true,
          });
        }
      });
    },
    //查询列表
    searchList() {
      let params = {
        pageNum: this.pageNum,
        pageSize: this.pageSize,
      };
      for (let key in this.searchParams) {
        if (this.searchParams[key]) {
          this.$set(params, key, this.searchParams[key]);
        }
      }
      searchApi(params).then((res) => {
        if (res.code === 200) {
          this.dataArr = res.data.records.map((item) => ({
            ...item,
            checked: false,
          }));
          this.total = res.data.total;
        } else {
          this.$Message.warning({
            content: res.msg,
            duration: 6,
            closable: true,
          });
        }
      });
    },
    //手动排程
    handleChange(status, type) {
      this.schedulingType = type
      this.diaStatus = status;
      if (this.delIds.length !== 1) {
        this.$Message.warning({
          content: '只能选择一条数据',
          duration: 6,
          closable: true,
        });
        return;
      }
      this.create = true;
      this.deviceList = [];
      this.moldListRow = [];
      this.formItem = {
        workStartTime: '',
        workEndTime: '',
        planProduceQuantity: this.delIds[0].planQuantity - this.delIds[0].workOrderSum,
      };
      this.rows = [];
      getMoldList({materialCode: this.delIds[0].orderMaterialCode}).then((res) => {
        if (res.code === 200) {
          this.moldList = res.data;
          this.create = true;
        } else {
          this.$Message.warning({
            content: res.msg,
            duration: 6,
            closable: true,
          });
        }
      });
    },
    fzpc() {
      this.setting = true;
    },
    // 编辑
    editOrder(status) {
      const delIds1 = JSON.parse(JSON.stringify(this.delIds1));
      this.schedulingType = 'auto'
      this.diaStatus = status;
      if (delIds1.length !== 1) {
        this.$Message.warning({
          content: '只能选择一条数据',
          duration: 6,
          closable: true,
        });
        return;
      }
      this.formItem.planProduceQuantity = null;
      this.formItem = {
        ...delIds1[0],
      };
      this.create = true;
      getMoldList({materialCode: delIds1[0].materialCode}).then((res) => {
        if (res.code === 200) {
          this.moldList = res.data;
          this.deviceList = [];
          this.create = true;
        } else {
          this.$Message.warning({
            content: res.msg,
            duration: 6,
            closable: true,
          });
        }
      });
    },
    //导出
    exportTemplateClick() {
      const filName = '工单台账';
      let params = {
        ...this.searchParams,
      };
      this.axios({
        method: 'post',
        url: '/dms/productionWork/exportWork',
        data: params,
        responseType: 'blob',
      }).then((res) => {
        fileDownload(res, filName + '.xlsx');
      });
    },
  },
};
</script>

<style scoped lang="scss">
.one_line {
  display: flex;
  flex-direction: row;

  .device_name {
    width: 90px;
    padding: 0 0 0 10px;
    text-overflow: ellipsis;
    overflow: hidden;
    white-space: nowrap;
  }

  .echart {
    flex: 1;
    margin-left: 10px;
  }
}

.echart_Box {
  position: relative;
  min-height: 200px;
}

.timeAxis {
  line-height: 25px;
}

.startTime {
  font-size: 12px;
  color: #333333;
  margin-left: 25px;
}

.endTime {
  font-size: 12px;
  color: #333333;
  float: right;
}

.selectCy {
  position: absolute;
  top: 18px;
  left: 100px;
}
</style>
